package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.service.INavService;
import com.cn.wanxi.util.IOTool;
import com.cn.wanxi.util.Path;
import com.cn.wanxi.util.StringTool;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NavThreeServiceImpl implements INavService {

    @Override
    public NavModel[] getNavModels(String path) {
//        1.创建文件

//        File file = IOTool.createNote(path, diretory, fileName);
//        2.写入数据
        List<String> data = getData();
        IOTool.writeNote(data, path, Path.FILE_NAME_NAV);
//        3.读取数据
        String result = IOTool.readNote();
//        4.封装数据
        //        5.返回数据
        return setData(result);
    }

    public static void main(String[] args) {
//        String aa="02899999";
//        System.out.println(Integer.parseInt(aa));
        new NavThreeServiceImpl().getNavModels("E:\\wanxi\\step-3\\canyin03-0809\\web");
    }

    /**
     * 封装数据
     *
     * @param result
     * @return
     */
    private NavModel[] setData(String result) {
//        所有数据来源于result
        String[] results = result.split(Path.SPLIT);
//        去掉字符串裡面不需要的字符?
//        String href = results[0];
//        href = StringTool.replace(href);
        String href = StringTool.replace(results[0]);
//        href = href.replace("[", "");
//        href = href.replace("]", "");
//        String title = results[1];
//        title = StringTool.replace(title);
        String title = StringTool.replace(results[1]);
        String[] hrefs = href.split(", ");
        String[] titles = title.split(", ");
        int length = hrefs.length;
        NavModel[] navModels = new NavModel[length];

        for (int i = 0; i < length; i++) {
            NavModel model = new NavModel();
            model.setTitle(titles[i]);
            model.setHref(hrefs[i]);
            navModels[i] = model;
        }
        return navModels;
    }

//    private String readNote(File file) {
//        return null;
//    }


    /**
     * 从数据访问层得到数据
     *
     * @return
     */
    private List<String> getData() {
        List<String> list = new ArrayList<>();
        NavDao navDao = new NavDao();
//        数组转换为字符串?
//        字符串转换为数组？split
        list.add(Arrays.toString(navDao.getHrefs()));
        list.add(Arrays.toString(navDao.getTitles()));
//        aa("","","","","","");
        return list;
    }
//    private void aa(String... aa){
//
//    }
//
//    private File createNote(String path, String diretory, String fileName) {
//        File dir = new File(path, diretory);
//        dir.mkdirs();
//        File file = new File(dir, fileName);
//        try {
//            file.createNewFile();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return file;
//    }
}
